package com.wedata.阿里;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import com.aliyun.tea.TeaException;
import com.disdr.utils.DateUtils;
import com.wedata.HttpUtils;

import java.util.Date;
import java.util.UUID;

public class 钉钉 {


    /**
     * 使用 Token 初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkworkflow_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkworkflow_1_0.Client(config);
    }

    public static void querySchema() throws Exception {
        com.aliyun.dingtalkworkflow_1_0.Client client = createClient();
        com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeHeaders querySchemaByProcessCodeHeaders = new com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeHeaders();
        querySchemaByProcessCodeHeaders.xAcsDingtalkAccessToken = postRequestWithSign();
        com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeRequest querySchemaByProcessCodeRequest = new com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeRequest()
                .setProcessCode("PROC-17428B8C-6C60-xxxx-924C-64F1037AE067")
                .setAppUuid("SWAPP-abcd-example");
        try {
            client.querySchemaByProcessCodeWithOptions(querySchemaByProcessCodeRequest, querySchemaByProcessCodeHeaders, new com.aliyun.teautil.models.RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }
    }
    public static void main(String[] args_) throws Exception {
        System.out.println(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date(1613059200000L)));
        //postRequestWithSign();
    }

    /**
     * 获取token
     * @return
     */
    public static String postRequestWithSign() {
        if("11".equals("1")){
            return "520247e8d964301dac469b7a1273267a";
        }
        JSONObject body = new JSONObject();
        String sign = MD5.create().digestHex(body +"#5601291336c76762f1d56ce5b45b2419");
        JSONObject header = new JSONObject();
        header.set("X-SIGN",sign);
        header.set("X-TRACEID", UUID.randomUUID());
        header.set("X-APPKEY", "10067615200");
        JSONObject result = HttpUtils.sendPost("https://wedata.gymcity.com.cn/openapi/third/getDingRobotToken", header, body);
        System.out.println(result);
        //System.out.println(result.getJSONObject("data").getStr("access_token"));
        return result.getJSONObject("data").getStr("access_token");
    }
}
